<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>Backbone的Model示例</title>
		<script src="js/jquery-1.8.3.min.js" type="text/javascript" charset="utf-8"></script>
		<script src="//cdn.bootcss.com/underscore.js/1.8.3/underscore-min.js"></script>
		<script src="//cdn.bootcss.com/backbone.js/1.2.3/backbone-min.js"></script>
	</head>
	<body>
		<script type="text/javascript">
			(function($){
			 	var man=Backbone.Model.extend({
			 		initialize:function(){
			 			//alert("hey you create me! thankyou");
			 			//3监听属性变化
				 		this.bind("change:name",function(){
				 			//alert("你修改了我的name属性");
				 		});
				 		this.bind("error",function(m,dd){
				 			 alert(dd);
				 		});
			 		},
			 		//1默认赋值方式
			 		defaults:{
			 			name:'xiaoming',
			 			age:12
			 		},
			 		//2对象中的方法
			 		aboutMe:function(){
			 			return "我叫"+this.get('name')+"我的年龄是"+this.get('age');
			 		},
			 		validate:function(attributes){
			 			if(attributes.name=='')
			 			{
			 				return "名字不能为空啊";
			 			}
			 		}
			 	});
			 	var m=new man;
			 	m.set({name:''});	
//			 	alert(m.get('name'));
 			 //	alert(m.aboutMe());
 			 
 			 
 			 
             //5对象的获取和保存需要后台配合
              var animal=Backbone.Model.extend({
              	url:'',
              	initialize:function() {
              		 this.bind("change:name",function(model,value){
              		 	console.log("you changed attribute name is "+value);
              		 });
              		 this.bind("error",function(model,erro){
              		 	console.log("name is  null place you wirtes name");
              		 })
              	},
              	defaults:{
              		name:'小狗',
              		age:5,
              		weight:"8kg",
              		color:'yello'
              	},
              	validate:function(model){
              		if(model.name=='')return 'name is null';
              	}
              });  
             var cat=new animal;
               cat.set("name","aa");
               //提交一种提交方式
               cat.save();
               //获取响应的数据,如果直接调用该方法会直接提交，
               cat.fetch(function(){});
               
                //第二种提交方式
               cat.fetch({url:'',seuccess:function(model,response){ },error:function(){}});
               
               
               
			 })(jQuery)
		</script>
	</body>

</html>